﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Model;
using BLL;
public partial class WebControl_MessageList : System.Web.UI.UserControl
{
    private MessageState _state; 
    public MessageState state
    {
        set { _state = value; }
        get { return _state; }
    }
    /// <summary>
    /// 析构
    /// </summary>
    public WebControl_MessageList()
    {
        _state = MessageState.Unread;
    }
    protected void PageOn1_PageChanged(object sender, EventArgs e)
    {
        ShowMessage();
    }
    /// <summary>
    /// 显示第几页内容
    /// </summary>
    /// <param name="pageNO"></param>
    private void ShowMessage()
    {
        int pagesize = 10;  
        SessionUser sUser = BaseLogin.getSessionNode(HttpContext.Current);
        BLL.MessageBLL bll = new MessageBLL(); 
        int totalCount = bll.GetMessageCount(sUser.uid,Convert.ToInt32( _state));
        
        this.PageOn1.total = totalCount;
        this.PageOn1.pageSize = pagesize;
        int pageNO = this.PageOn1.currentPage; 

        this.rptUnread.DataSource = bll.GedMessageList(pagesize, pageNO, sUser.uid, Convert.ToInt32(_state));
        this.rptUnread.DataBind();


    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if(_state == MessageState.Unread)
                this.labHead.Text =  "<li class=\"current\">未读</li>" +
                            " <li onclick=\"window.location.href='MessageRead.aspx'\">已读</li> ";
            else
                this.labHead.Text = "<li onclick=\"window.location.href='Message.aspx'\" >未读</li>" +
                            " <li class=\"current\">已读</li> ";
            //显示第一页的内容
            ShowMessage();
        }
        
    }
    /// <summary>
    /// 删除一条消息
    /// </summary>/
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Delete_Click(object sender, EventArgs e)
    {
        LinkButton lb = (LinkButton) sender;
        if(Common.Help.IsNumber(lb.CommandArgument.ToString()))
        {
            int mesId = int.Parse(lb.CommandArgument.ToString());
            BLL.MessageBLL bll = new MessageBLL();
            SessionUser sUser = BaseLogin.getSessionNode(HttpContext.Current);
            if (bll.Delete(sUser.uid, mesId))
            {
                ShowMessage();
            }
        }
        else
        {
            return ;
        }
        
    }
}
